import { getStream } from '../Core/connect'
import { Object3D } from './Object3D'
import { Vector3 } from '../Math/Vector3'
import { Material } from '../Material/Material'
import * as MathUtils from '../Math/MathUtils'
type actorState = 'setNewTime' |'reverseEnd'|'reverse'|'stop'|'play'|'replay'
type pipeType = 'square'|'pipe'         // 正方形、管道
export class Spline extends Object3D {
    readonly type: string
    constructor(name: string) {
        super({name: name, type: 'spline'})
        this.type = 'spline'
    }
    // 用户Alt+鼠标点击构线
    clickAddSpline(lineWidth:Number = 10){
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const str = {splineFunction: 'clickAddSpline',sessionId:sessionId,lineWidth:lineWidth}
            stream.emitUIInteraction(str)
            const clickAddSpline = (data: any) => {
                const returnData = JSON.parse(data)
                if(returnData.sessionId === sessionId){
                    console.log('线集合',returnData)
                    resolve(returnData)
                }
            }
            stream.addResponseEventListener(`clickAddSpline`, clickAddSpline)
        })
    }
    // 点集构建spline(UE坐标)
    addPointList(pointList:Array<Vector3>) {
        const stream = getStream()
        const str = {splineFunction: 'addPointList', ids: `${this.uuid}`, pointList:pointList}
        stream.emitUIInteraction(str)
    }
    // 点集构建管道(UE坐标)
    addPipeByList(pointList:Array<Vector3>,width:string,Material: Material, pipeType:pipeType = 'square') {
        const stream = getStream()
        const str = {splineFunction: 'addPipeByList', ids: `${this.uuid}`, pointList:pointList, Material:Material, materialType: Material.type, width:width, pipeType: pipeType}
        stream.emitUIInteraction(str)
    }
    
    // 点集构建管道(经纬度)
    addPipeByCoorList(pointList:Array<Vector3>,width:string,Material: Material,  pipeType:pipeType = 'square') {
        const stream = getStream()
        const str = {splineFunction: 'addPipeByCoorList', ids: `${this.uuid}`, pointList:pointList, Material:Material, materialType: Material.type, width:width, pipeType: pipeType}
        stream.emitUIInteraction(str)
    }
    // 删除管道
    deletePipe(){
        const stream = getStream()
        const str = {splineFunction: 'deletePipe', ids: `${this.uuid}`}
        stream.emitUIInteraction(str)
    }
    // 修改线条宽度
    changeSplineWidth(width:String) {
        const stream = getStream()
        const str = {splineFunction: 'changeSplineWidth', ids: `${this.uuid}`,width:width, m: Material, }
        stream.emitUIInteraction(str)
    }
    // 修改线条颜色：基础材质
    changeSplineMaterial(Material: Material) {
        const stream = getStream()
        const str = {splineFunction: 'changeSplineMaterial', ids: `${this.uuid}`, Material: Material, materialType: Material.type}
        stream.emitUIInteraction(str)
    }
    // 修改线条材质
    // changeSplineImgFloor(splineMaterial:Material) {
    //     const stream = getStream()
    //     const str = {splineFunction: 'changeSplineImgFloor', ids: `${this.uuid}`, material:splineMaterial}
    //     stream.emitUIInteraction(str)
    // }
    // 添加点
    addPoint(position: Vector3,index:string){
        this.position = position
        const stream = getStream()
        const str = {splineFunction: 'addPoint', ids: `${this.uuid}`,position:`${this.position.x},${this.position.y},${this.position.z}`,index:`${index}`}
        stream.emitUIInteraction(str)
    }
    // 删除点
    removeIndexPoint(index:string){
        const stream = getStream()
        const str = {splineFunction: 'removeIndexPoint', ids: `${this.uuid}`,index:`${index}`}
        stream.emitUIInteraction(str)
    }
    // actor沿spline移动:cameraFollow为true,才需要cameraFollowDistance;updataRotation是否更新角度;runListen是否开启运动时监听
    moveActorBySpline(actorId:string,speed:Number,cameraFollow:boolean,cameraFollowDistance:Number = 5000,updataRotation:boolean = true,runListen:boolean = false,fun?:Function){
        const that = this
        const stream = getStream()
        const str = {splineFunction: 'moveActorBySpline', ids: `${that.uuid}`,actorId:`${actorId}`,speed:`${speed}`,cameraFollow:`${cameraFollow}`,cameraFollowDistance:cameraFollowDistance,updataRotation:updataRotation,runListen:runListen}
        stream.emitUIInteraction(str)
        const actorMoveEnd = (data: any) => {
            const returnData = JSON.parse(data)
            if(returnData.ids === that.uuid){
                //确定是相对应的回话
                if(fun){fun(returnData)}
            }
        }
        stream.addResponseEventListener("actorMoveEnd", actorMoveEnd)
    }
    // 修改运动状态
    changeState(state:actorState){
        const stream = getStream()
        const str = {splineFunction: 'changeState', ids: `${this.uuid}`,state:`${state}`}
        stream.emitUIInteraction(str)
    }
    // 修改运动速度
    changeSpeed(newSpeed:Number){
        const stream = getStream()
        const str = {splineFunction: 'changeSpeed', ids: `${this.uuid}`,newSpeed:`${newSpeed}`}
        stream.emitUIInteraction(str)
    }
    // 设置指定时刻下的物体位置
    setNewTime(newTime:Number){
        const stream = getStream()
        const str = {splineFunction: 'setNewTime', ids: `${this.uuid}`,newTime:`${newTime}`}
        stream.emitUIInteraction(str)
    }
    
}